package com.fengke.csc.action.tag;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.fengke.csc.action.BaseAction;
import com.fengke.dao.model.TagModel;
import com.fengke.service.CacheService;
import com.fengke.service.TagService;
import com.fengke.service.vo.TagVO;
import com.fengke.util.Constants;
import com.fengke.util.LoggerFactory;
import com.fengke.util.StringUtil;

public class TagAction extends BaseAction {
	private static final long serialVersionUID = 7851726358993621515L;
	private Logger logger = LoggerFactory.getLogger(TagAction.class);
	private TagService tagService;
	private CacheService cacheService;
	private List<TagVO> tagVOList; // 标签列表
	private TagVO tagVO = new TagVO();
	
	public String tagManager() {
		logger.debug("Method: tagManager invoke...");
		if(tagVO == null) {
			tagVO = new TagVO();
		}
		if(tagVO.getTagModel() == null) {
			tagVO.setTagModel(new TagModel());
		}
		tagVO.getTagModel().setType(TagModel.Type.product);
		try {
			tagVOList = tagService.researchTagVOList(tagVO);
		} catch (Exception e) {
			logger.error("tagManager researchTagVOList Exception! tagVO=" + tagVO, e);
			this.addPageDangerMessage("获取标签列表失败！");
		}
		
		return Constants.ActionReturnName.SUCCESS;
	}
	public String createTag() {
		if(tagVO == null || tagVO.getTagModel() == null || StringUtil.isEmpty(tagVO.getTagModel().getName())) {
			this.addPageDangerMessage("关键参数不能为空！");
			return Constants.ActionReturnName.SUCCESS;
		}
		tagVO.getTagModel().setCreateDate(new Date());
		tagVO.getTagModel().setModifyDate(new Date());
		tagVO.getTagModel().setType(TagModel.Type.product);
		try {
			tagVO = tagService.createTag(tagVO);
		} catch (Exception e) {
			logger.error("createTag Exception! tagVO=" + tagVO, e);
			this.addPageDangerMessage("添加失败！");
		}
		if(!this.hasPageDangerMessage()) {
			this.addPageSuccessMessage("添加成功！");
		}
		
		try {
			cacheService.invalidTagCache();
		} catch (Exception e) {
			logger.error("invalidTagCache Exception!", e);
		}
		return Constants.ActionReturnName.SUCCESS;
	}
	public String updateTag() {
		if(tagVO == null || tagVO.getTagModel() == null || tagVO.getTagModel().getId() == null || StringUtil.isEmpty(tagVO.getTagModel().getName())) {
			this.addPageDangerMessage("关键参数不能为空！");
			return Constants.ActionReturnName.SUCCESS;
		}
		tagVO.getTagModel().setModifyDate(new Date());
		try {
			tagVO = tagService.updateTag(tagVO);
		} catch (Exception e) {
			logger.error("updateTag Exception! tagVO=" + tagVO, e);
			this.addPageDangerMessage("更新失败！");
		}
		if(!this.hasPageDangerMessage()) {
			this.addPageSuccessMessage("更新成功！");
		}
		try {
			cacheService.invalidTagCache();
		} catch (Exception e) {
			logger.error("invalidTagCache Exception!", e);
		}
		return Constants.ActionReturnName.SUCCESS;
	}
	public String deleteTag() {
		// 获取ID列表
		String deletedIds = ((String[]) (this.getValue().get("deletedTagIds")))[0];
		if(deletedIds == null || StringUtil.isEmpty(deletedIds)) {
			return Constants.ActionReturnName.SUCCESS;
		}
		String [] ids = deletedIds.split(" ");
		try {
			tagService.deleteTagByIds(Arrays.asList(ids));
		} catch (Exception e) {
			logger.error("删除标签失败！ids=" + ids, e);
			this.addPageDangerMessage("删除标签失败！");
		}
		if(!this.hasPageDangerMessage()) {
			this.addPageSuccessMessage("删除成功！");
		}
		try {
			cacheService.invalidTagCache();
		} catch (Exception e) {
			logger.error("invalidTagCache Exception!", e);
		}
		return Constants.ActionReturnName.SUCCESS;
	}
	public Logger getLogger() {
		return logger;
	}
	public void setLogger(Logger logger) {
		this.logger = logger;
	}
	public TagService getTagService() {
		return tagService;
	}
	public void setTagService(TagService tagService) {
		this.tagService = tagService;
	}
	public CacheService getCacheService() {
		return cacheService;
	}
	public void setCacheService(CacheService cacheService) {
		this.cacheService = cacheService;
	}
	public List<TagVO> getTagVOList() {
		return tagVOList;
	}
	public void setTagVOList(List<TagVO> tagVOList) {
		this.tagVOList = tagVOList;
	}
	public TagVO getTagVO() {
		return tagVO;
	}
	public void setTagVO(TagVO tagVO) {
		this.tagVO = tagVO;
	}
}
